package cn.pugle.spring.spel;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * <p>
 *
 * @auther tzp on 2016/8/3.<br>
 */
public class SpelTest {
    public static void main(String[] args) {
        Apple apple = new Apple();
        ExpressionParser parser = new SpelExpressionParser();

        Expression exp = parser.parseExpression("'Hello World'.concat('!')");//调用对象方法
        System.out.println(exp.getValue());

        exp = parser.parseExpression("name");//直接取对象属性. 私有的不行
        System.out.println(exp.getValue(apple));

        exp = parser.parseExpression("size");
        System.out.println(exp.getValue(apple));//通过size取getSize访问器方法

        EvaluationContext evalContext = new StandardEvaluationContext(apple);//标准表达式执行上下文， 使用反射获取属性、方法
        evalContext.setVariable("aapple", new Apple());

        exp = parser.parseExpression("#aapple.size");//取不是根object的object的属性
        System.out.println(exp.getValue(evalContext));


//       todo ConstructorResolvers  MethodResolvers PropertyAccessors 扩展spel对方法属性的访问
    }
}
class Apple {
    public String name = "Big Apple";
    private String myName = "a little apple";
    public String getSize(){
        return "1kg";
    }
}
